#!/bin/gawk -f
#
#   jfd@21/06/96
#   gawk script to transform  Regine/Biosym distance restraints list 
#   to X-plor format
#   A check will be made for:
#	*   lower_bound > upper_bound
#	*   R/S nomenclature (will be converted to xplor 1/2 = reverse IUPAC)
#   Upper limit will be the target + upper_dev!!!
#   Lower limit will be 1.8 if not defined by -1 flag
#
#   usage: "reg2xplor file.in > file.out"

BEGIN {
	number_of_restrains = 0
	lower_upper_errors  = 0
	lower_limit_stan    = 1.8
	upper_dev	    = 0.0
	printf("# total number of restrains given at end of file\n", 
	       number_of_restrains)

      }

/^1/{
	gsub(":", " ")
	gsub("_", " ")
	res_type[1]  = $2
	res_number[1]= $3
	atom[1]	     = $4
	res_type[2]  = $6
	res_number[2]= $7
	atom[2]	     = $8
	lower_limit  = $9
	target	     = $10

	if ( (lower_limit < 0)||(lower_limit == -1) ) {
		lower_limit = lower_limit_stan
	}

	if (target < lower_limit) {
	    print "ERROR target < lower_limit"
 	    printf("between %s %s and %s %s at line %d\n",
		res_number_1, atom_1, res_number_2, atom_2, NR)
	    target = lower_limit + 0.3
	    lower_upper_errors++
	}
	lower_dev   = target - lower_limit
	number_of_restrains++

for (i=1;i<=2;i++) {
 res_type[i] = substr(res_type[i],1,3)
 if ( (index(atom[i],"R") > 0) || (index(atom[i],"S") > 0) ) {
 	old_atom = atom[i]
# HA
	if (atom[i]=="HAR") atom[i]="HA2"
	if (atom[i]=="HAS") atom[i]="HA1"
# HB
	if ( substr(atom[i],1,2)=="HB") {
#     PRO, ARG, ASN, ASP, CYS, GLN, GLU, HIS, LEU, LYS, MET, PHE, SER, TRP, TYR
		flag = 0
		if ( (res_type[i]=="ASN") || (res_type[i]=="ASP") ) flag = -1
		if ( (res_type[i]=="HIS") || (res_type[i]=="MET") ) flag = -1
		if ( (res_type[i]=="SER")                         ) flag = -1
		if (flag == 0) {
			if (atom[i]=="HBR") atom[i]="HB2"
			if (atom[i]=="HBS") atom[i]="HB1"
		} else {
			if (atom[i]=="HBR") atom[i]="HB1"
			if (atom[i]=="HBS") atom[i]="HB2"
		}
	}
# HG
	if ( substr(atom[i],1,2) == "HG") {
#	PRO, ARG, GLN, GLU, LYS, MET
		if (res_type[i] == "LYS") {
			if (atom[i]=="HGR") atom[i]="HG2"
			if (atom[i]=="HGS") atom[i]="HG1"
		} else {
			if (atom[i]=="HGR") atom[i]="HG1"
			if (atom[i]=="HGS") atom[i]="HG2"
		}
# methyl VAL
		if (res_type[i] == "VAL") {
			if (atom[i]=="HGR*") atom[i]="HG1*"
			if (atom[i]=="HGS*") atom[i]="HG2*"
		}
	}
# HD
	if ( substr(atom[i],1,2) == "HD") {
#	PRO, ARG, LYS
			if (atom[i]=="HDR") atom[i]="HD2"
			if (atom[i]=="HDS") atom[i]="HD1"
# methyl LEU
		if (res_type[i]=="LEU") {
			if (atom[i]=="HDR*") atom[i]="HD1*"
			if (atom[i]=="HDS*") atom[i]="HD2*"
		}
	}
	if (old_atom != atom[i]) {
#		printf("Residue %s , atom %s mapped to %s\n", res_type[i], old_atom, atom[i])
	}
 }
}		

	printf("assign (resid  %4d and name %4s) (resid  %4d and name %4s) ",
	  res_number[1], atom[1], res_number[2], atom[2])
	printf("%5.3f %5.3f %5.3f\n", target, lower_dev, upper_dev)

    }
!/^1/ {
	printf("#%s\n", $0);
}

END {
    printf("# REPORT\n")
    printf("# Total number of restrains             : %d\n", number_of_restrains)
    printf("# Number of bad upper bounds (adjusted) : %d\n", lower_upper_errors)

}
